/*
 * This file is part of Smart Route for S60 3rd Edition terminals
 * 
 * Copyright (C) 2006  Harri Salminen

 * This program free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 *(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <avkon.hrh>
#include <avkon.rsg>
#include <eikmenup.h>
#include <aknappui.h>
#include <eikcmobs.h>
#include <barsread.h>
#include <stringloader.h>
#include <gdi.h>
#include <eikedwin.h>
#include <eikenv.h>
#include <eikmfne.h>
#include <eikfpne.h>

#include "TripInfo.h"
#include "SmartRoute.rsg"
#include "SmartRoute.hrh"
#include "MainScreen.hrh"
#include "TripInfo.hrh"

CTripInfo* CTripInfo::NewL(MEikCommandObserver* aCommandObserver, CSmartRoute* aRoute, const TRect& aRect)
	{
	CTripInfo* self = CTripInfo::NewLC(aCommandObserver, aRoute, aRect);
	CleanupStack::Pop(self);
	return self;
	}

CTripInfo* CTripInfo::NewLC(MEikCommandObserver* aCommandObserver, CSmartRoute* aRoute, const TRect& aRect)
	{
	CTripInfo* self = new (ELeave) CTripInfo(aCommandObserver, aRoute);
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}
	
void CTripInfo::ConstructL(const TRect& aRect)
	{
    CreateWindowL();
    SetRect(aRect);
    ActivateL();
	}

void CTripInfo::SetContainerL()
	{
	if(iRouteForm)
		{
		delete iRouteForm;
		iRouteForm = NULL;
		}
		
	iRouteForm = CTripInfoForm::NewL(); 
    iRouteForm->PrepareLC(R_TRIP_INFO_TRIP_INFO);
    iRouteForm->SetDataL(iRoute); 
    iRouteForm->RunLD();
	}
		 
TKeyResponse CTripInfo::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType )
	{
	if ( ( aKeyEvent.iCode == EKeyLeftArrow || aKeyEvent.iCode == EKeyRightArrow ))
		{
		// allow the tab control to get the arrow keys
		return EKeyWasNotConsumed;
		}
		
	switch(aKeyEvent.iScanCode)
		{
		case EKeyCBA1:
			ProcessCommandL(ESmartRouteExport);
			return EKeyWasConsumed;
			break;
		case EKeyCBA2:
		case 165:
			ProcessCommandL(ESmartRouteBack);
			return EKeyWasConsumed; 
			break;
		default:
			return iRouteForm->OfferKeyEventL(aKeyEvent, aType);
		}

	}
				
CTripInfo::~CTripInfo()
	{
	delete iRouteForm;
	delete iRoute;
	}
	
				
void CTripInfo::ProcessCommandL(TInt aCommandId)
	{
	iCommandObserver->ProcessCommandL(aCommandId);
	}
	
CTripInfo::CTripInfo( MEikCommandObserver* aCommandObserver, CSmartRoute* aRoute  )
	{
	iCommandObserver = aCommandObserver;
	iRoute = aRoute;
	}
	
TInt CTripInfo::CountComponentControls() const
	{
	return 1;
	}
	
CCoeControl* CTripInfo::ComponentControl( TInt aIndex ) const
	{
	switch(aIndex)
		{
		case 0: 
			return iRouteForm;
			break;
		default: 
			return NULL;
		}
	}

CTripInfoForm* CTripInfoForm::NewL()	
	{
	CTripInfoForm* self = new (ELeave) CTripInfoForm();
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(); //self
	return self;
	}

CTripInfoForm::CTripInfoForm()
	{
	}
	
void CTripInfoForm::ConstructL()
	{
	CAknForm::ConstructL();
	}
	
 void CTripInfoForm::SetDataL(CSmartRoute* aRoute)
 	{
 	if(!aRoute)
 		User::Leave(KErrArgument);
 		
 	iRoute = aRoute;
 	
 	iTripName = static_cast< CEikEdwin* >( 
		ControlOrNull( ETripInfoViewTripName ) );
		{
		HBufC* text = StringLoader::LoadLC( R_TRIP_INFO_TRIP_NAME );
		iTripName->SetTextL( text );
		CleanupStack::PopAndDestroy( text ); 
		}
	iTripDuration = static_cast< CEikDurationEditor* >( 
		ControlOrNull( ETripInfoViewTripDuration ) );
	iTripDuration->SetDuration( TTimeIntervalSeconds( 0 ) );
	iTripStart = static_cast< CEikTimeAndDateEditor* >( 
		ControlOrNull( ETripInfoViewTripStart ) );
	iTripStart->SetTimeAndDate( TTime( TDateTime( 2000, EJanuary, 0, 0, 0, 0, 0 ) ) );
	iTripEnd = static_cast< CEikTimeAndDateEditor* >( 
		ControlOrNull( ETripInfoViewTripEnd ) );
	iTripEnd->SetTimeAndDate( TTime( TDateTime( 2000, EJanuary, 0, 0, 0, 0, 0 ) ) );
	/*
	iTripLength = static_cast< CEikFixedPointEditor* >( 
		ControlOrNull( ETripInfoViewTripLength ) );
		{
		TInt value = 0;
		iTripLength->SetValueL( &value );
		}
	iTripAvgSpeed = static_cast< CEikFixedPointEditor* >( 
		ControlOrNull( ETripInfoViewTripAvgSpeed ) );
		{
		TInt value = 0;
		iTripAvgSpeed->SetValueL( &value );
		}
	iTripMaxSpeed = static_cast< CEikFixedPointEditor* >( 
		ControlOrNull( ETripInfoViewTripMaxSpeed ) );
		{
		TInt value = 0;
		iTripMaxSpeed->SetValueL( &value );
		}
	*/
	/*
	 * Set data
	 */
	 
	iTripStart->SetTimeAndDate(iRoute->Start());
	iTripEnd->SetTimeAndDate(iRoute->End());
	iTripName->SetTextL(&(iRoute->Name()));
	iTripDuration->SetDuration(iRoute->Duration());
	
	/*
	TLex lex;
	TInt distance, maxSpeed;
	TReal32 real;
	real = iRoute->Distance(); distance = lex.Val(real);
	real = iRoute->MaxSpeed(); maxSpeed = lex.Val(real);
	iTripLength->SetValueL(&distance); 
	iTripMaxSpeed->SetValueL(&maxSpeed);
	*/
 	}
 
 
 void CTripInfoForm::DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane)
 	{
 	if (aResourceId == R_AVKON_FORM_MENUPANE)
		{
		aMenuPane->SetItemDimmed( EAknFormCmdAdd, ETrue );
		aMenuPane->SetItemDimmed( EAknFormCmdLabel, ETrue );
		aMenuPane->SetItemDimmed( EAknFormCmdDelete, ETrue );
		aMenuPane->SetItemDimmed( EAknFormCmdEdit, ETrue );
		aMenuPane->SetItemDimmed( EAknFormCmdSave, ETrue );
		}
 	}


